const wx2my = require('../../wx2my');

// pages/recharge/recharge.js
const app = getApp();
let page = 1;
let totalpage;
Page({
  data: {
    onOff: 0,
    success: true,
    list: [],
    select: 1,
    active: 1,
    type: 3,
    id: ''
  },

  //价格选择
  active(e) {
    console.log(e);

    if (e.currentTarget.dataset.select == 1) {
      return false;
    }

    var that = this;
    let list = that.data.list;
    let select = that.data.select;
    let index = e.currentTarget.dataset.index;
    console.log(select);
    list.forEach((item, index) => {
      if (index == e.currentTarget.dataset.index) {
        item.select = 1;
      } else {
        item.select = 0;
      }
    });
    let status = e.currentTarget.dataset.status;
    let id = e.currentTarget.dataset.id;
    console.log(id);
    that.setData({
      list: list,
      id: id
    });
  },

  onLoad: function (e) {},
  onReady: function () {
    page = 1;
    this.get_list();
  },
  onShow: function () {},

  get_list() {
    let that = this;
    app.wxRequest('recharge_list', {
      'page': page
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        let list = res.data.data.list;
        const regexp = /(?:\.0*|(\.\d+?)0+)$/;
        res.data.data.list.forEach(item => {
          item.recharge_money = item.recharge_money.replace(regexp, '$1');
          item.give_money = item.give_money.replace(regexp, '$1');
        });

        if (page == 1) {
          totalpage = res.data.data.totalpage;
        }

        that.setData({
          list: page == 1 ? list : that.data.list.concat(list),
          lock: totalpage <= page ? true : false
        });
      }
    });
  },

  buy(e) {
    let that = this;

    if (that.data.id == '') {
      app.toast("请选择充值套餐");
    } else {
      app.wxRequest('recharge_pay', {
        'token': wx2my.getStorageSync('token'),
        'type': 2,
        'recharge_id': that.data.id
      }, function (res) {
        console.log(res);
        console.log(res.data.code);
        console.log(res.data.data.payparams.trade_no);

        if (res.data.code == 1) {
          
          my.tradePay({
            tradeNO: res.data.data.payparams.trade_no, // 完整的支付参数拼接成的字符串，从 alipay.fund.auth.order.app.freeze 接口获取
            success: (res) => {
              console.log(res);
              that.setData({
                success: false
              });
              setTimeout(() => {
                wx2my.navigateBack();
              }, 2000);
            },
            fail: (res) => {
              that.setData({
                onOff: 0
              });
              console.log(err);
            }
          });


          
        } else {
          app.toast(res.data.msg);
          that.setData({
            onOff: 0
          });
          setTimeout(() => {
            wx2my.navigateBack();
          }, 2000);
        }
      });
    }
  },

  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {}
});